#include "comm_rinnai.h"
#include "machine_stat.h"
#include "sc95f.h"
#include "string.h"
#include "display.h"
#include "key_process.h"
#include "sc95f_usci2.h"
#include "my_uart.h"
#include "SC_Init.h"	// MCU initialization header file, including all firmware library header files
#include "SC_it.h"
uint8_t comm_send_data[60]={0};
uint8_t comm_receive_data[60]={0};
union transmit_data_union  tran_data;
union receive_data_union   rec_data;
uint8_t rx_count=0 ;
uint8_t	rx_buff1[21]={0};
uint8_t	rx_buff[21]={0};
uint8_t	rx_buff_old[21]={0};
uint8_t data_upgrate_flg=0;
//uint8_t comm_tran_over_flg=1;
uint8_t ring_status_old=0;
uint8_t ring_status=0;
uint8_t key_wind_status=0;
uint8_t key_wind_status_old=0;
uint8_t key_self_status=0;
uint8_t key_self_status_old=0;
uint8_t key_fry_status=0;
uint8_t key_fry_status_old=0;
uint8_t key_inc_status=0;
uint8_t key_inc_status_old=0;
uint8_t key_rec_status=0;
uint8_t key_rec_status_old=0;
uint8_t key_timing_status=0;
uint8_t key_timing_status_old=0;

uint8_t Aip33624_diffuse_display_flg=0;
uint8_t Aip33624_diffuse_one_display_flg=0;
uint8_t Aip33624_ring_auto_show_flg=0;
uint8_t Aip33624_ring_auto_one_show_flg=0;
uint8_t Aip33624_ring_auto_one_show_end_flg=0;
uint8_t Aip33624_breath_led_control_flg=0;
uint8_t comm_200ms_wait_time_set=4;
//uint8_t com_rec_flg=0;


uint8_t tx_buff_err=0;

uint8_t tx_buff_err_count=0;

uint8_t rx_check=0;
uint8_t tx_check=0;
uint8_t TX_CheckSum(uint8_t *buf, uint8_t len) //buf为数组，len为数组长度
{
    uint8_t i, ret = 0;

    for(i=0; i<len; i++)
    {
        ret += *(buf++);
    }
     ret = ~ret;
    return ret;
}
uint8_t TX_CheckSum1(uint8_t *buf, uint8_t len) //buf为数组，len为数组长度
{
    uint8_t i, ret = 0;

    for(i=0; i<len; i++)
    {
        ret += *(buf++);
    }
     ret = ~ret;
    return ret;
}
uint8_t RX_CheckSum(uint8_t *buf, uint8_t len) //buf为数组，len为数组长度
{
    uint8_t i, ret = 0;

    for(i=0; i<len; i++)
    {
        ret += *(buf++);
    }
     ret = ret;
    return ret+1;
}



void uart_reset(void)
{
	GPIO_Init(GPIO1, GPIO_PIN_4,GPIO_MODE_IN_PU);
	GPIO_Init(GPIO1, GPIO_PIN_5,GPIO_MODE_OUT_PP);
	P15=1;
	USCI2_ITConfig(DISABLE,LOW);
	
	tx_count1=1;
	USCI2_ClearFlag(USCI2_UART_FLAG_TI);
	tran_com_end=1;
	USCI2_ClearFlag(USCI2_UART_FLAG_RI);
	rx_count=0;
	rx_end_flg=0;												
	USCI2_UART_Init(32000000,9600,USCI2_UART_Mode_10B,USCI2_UART_RX_ENABLE);
	USCI2_ITConfig(ENABLE,LOW);

}

uint8_t tran_lent=0;
uint8_t tran_com_end=1;
uint8_t tran_com_wait_flg=0;

uint8_t button_short_flg=0;
uint8_t button_short_count=0;
uint8_t button_long_flg=0;
void comm_auto_tran(void)
{

	tran_data.tran_data.Frame_header=0xaa;
	tran_data.tran_data.Machine_type=0x06;
	tran_data.tran_data.Data_length=TX_BUF_SIZE-1;
	tran_data.tran_data.Command_id=0x01;
	tran_data.tran_data.Data_buff[0]=0x00;
	tran_data.tran_data.Data_buff[1]=0x00;//按键短按 加=0x20，减=0x10，风速=0x08，定时=0x04，自动=0x02，爆炒=0x01
	
	
//	if(button_short_flg)
//	{
//		
//		if(button_short_count<1)
//		{
//			button_short_count+=1;
//		}else
//		{
//			button_short_flg=0;
//			tran_data.tran_data.Data_buff[1]=0x00;//按键短按 加=0x20，减=0x10，风速=0x08，定时=0x04，自动=0x02，爆炒=0x01
//		}

//	}
	
	if(button_long_flg==0)
	{
	//	tran_data.tran_data.Data_buff[1]=0x00;
		tran_data.tran_data.Data_buff[2]=0x00;//按键长按 加=0x20，减=0x10，风速=0x08，定时=0x04，自动=0x02，爆炒=0x01
		comm_200ms_wait_time_set=4;
	}
	
//	if(tx_buff_err&0x08)
//	{
//		tx_buff_err_count+=1;
//	}
//	if(tx_buff_err_count>=2)
//	{
//		tx_buff_err=tx_buff_err&0xf7;
//	}
	tran_data.tran_data.Data_buff[3]=tx_buff_err;//头部控制版状态
	tran_data.tran_data.Data_buff[4]=0x04;//风机最大挡位
	tran_data.tran_data.Data_buff[5]=0x00;//旋钮状态
	tran_data.tran_data.Data_buff[6]=0x00;//头部pcb温度值
	tran_data.tran_data.Data_buff[7]=0x4A;//产品配置信息1
	tran_data.tran_data.Data_buff[8]=0x00;//预留
	tran_data.tran_data.Data_buff[9]=0x00;//预留
	tran_data.tran_data.Data_buff[10]=0x00;//预留
	tran_data.tran_data.Data_buff[11]=0x00;//预留
	tran_data.tran_data.Data_buff[12]=version;//版本

	tran_data.tran_data.Checkout=TX_CheckSum(tran_data.tx_buffer,TX_BUF_SIZE-1);
	tran_lent=TX_BUF_SIZE;
	tran_com_end=0;
		
	

		US2CON3 = 0xAA;
		tx_count1=1;
		

}

//void comm_auto_tran(void)
//{

//	tran_data.tran_data.Frame_header=0xaa;
//	tran_data.tran_data.Machine_type=0x06;
//	tran_data.tran_data.Data_length=RX_BUF_SIZE-1;
//	tran_data.tran_data.Command_id=0x10;
//	tran_data.tran_data.Data_buff[0]=0x00;
//	tran_data.tran_data.Data_buff[1]=0xff;//按键短按 加=0x20，减=0x10，风速=0x08，定时=0x04，自动=0x02，爆炒=0x01
//	tran_data.tran_data.Data_buff[2]=0xff;//按键长按 加=0x20，减=0x10，风速=0x08，定时=0x04，自动=0x02，爆炒=0x01
//	tran_data.tran_data.Data_buff[3]=0xff;//头部控制版状态
//	tran_data.tran_data.Data_buff[4]=0xff;//风机最大挡位
//	tran_data.tran_data.Data_buff[5]=0xff;//旋钮状态
//	tran_data.tran_data.Data_buff[6]=0xff;//头部pcb温度值
//	tran_data.tran_data.Data_buff[7]=0x05;//产品配置信息1
//	tran_data.tran_data.Data_buff[8]=0xff;//预留
//	tran_data.tran_data.Data_buff[9]=0xff;//预留
//	tran_data.tran_data.Data_buff[10]=0x00;//预留
//	tran_data.tran_data.Data_buff[11]=0x00;//预留
//	tran_data.tran_data.Data_buff[12]=0x00;//版本
//	tran_data.tran_data.Data_buff[13]=0x00;//版本
//	tran_data.tran_data.Data_buff[14]=0x00;//版本
//	tran_data.tran_data.Data_buff[15]=0x12;//版本
//	
//	tran_data.tran_data.Checkout=TX_CheckSum(tran_data.tx_buffer,RX_BUF_SIZE-1);
//	tran_lent=RX_BUF_SIZE;
//	
//	tran_com_end=0;
//		US2CON3 = 0xAA;

//}

uint8_t button_tran_buff=0;
uint8_t button_tab_num=0;
void comm_custom_tran(uint8_t tab_num,uint8_t value)
{
	uint8_t wait_time=0xff;
	comm_200ms_wait_time=0;
	tran_data.tran_data.Data_buff[tab_num]=value;
//	button_tran_buff=value;
//	button_tab_num=tab_num;
	tran_data.tran_data.Checkout=TX_CheckSum(tran_data.tx_buffer,TX_BUF_SIZE-1);
	if(tran_com_end==1){
//	tran_data.tran_data.Data_buff[tab_num]=	button_tran_buff;
//	tran_data.tran_data.Checkout=TX_CheckSum(tran_data.tx_buffer,TX_BUF_SIZE-1);
	US2CON3 = 0xAA;
	}else
	{
	tran_com_wait_flg=1;
	}
	//uart_send(tran_data.tx_buffer,TX_BUF_SIZE);

}
uint8_t uart_wait_time_count=0;
uint8_t uart_wait_time=0;
uint8_t uart_wait_err_flg=0;
uint8_t uart_wait_err_flg2=0;
uint8_t uart_wait_err_2=0;
void comm_err_detection2(void)
	{
		if(uart_wait_err_flg2==1){
//			uart_wait_err_flg2=0;
			
		AiP33624_all_close();
		diffuse_count1=0;//扩散循环转扩散一次 计数清零
		atuo_count1=0;//圆环自动转圆环单次计数清零
		atuo_count=0;
		ring_delay_count_end_flg=0;		
		flow_delay_count_end_flg=0;						
		ring_delay_count=0;
		flow_delay_count=0;	
		P25=0;
		P27=0;		
	 ring_status_old=0;
	 ring_status=0;
	 key_wind_status=0;
	 key_wind_status_old=0;
	 key_self_status=0;
	 key_self_status_old=0;
	 key_fry_status=0;
	 key_fry_status_old=0;
	 key_inc_status=0;
	 key_inc_status_old=0;
	 key_rec_status=0;
	 key_rec_status_old=0;
	 key_timing_status=0;
	 key_timing_status_old=0;		
		
		Aip33624_ring_auto_show_flg=0;
		Aip33624_diffuse_display_flg=0;
		Aip33624_ring_auto_one_show_flg=0;
		Aip33624_diffuse_one_display_flg=0;
		Aip33624_breath_led_control_flg=0;
		Aip33624_ring_auto_one_show_end_flg=0;	
//		
//		memset(rx_buff,0,sizeof(rx_buff));
//		memset(rx_buff1,0,sizeof(rx_buff1));
//		memset(rx_buff_old,0,sizeof(rx_buff_old));
		
		AiP650_display(0x00,Aip_650_numb_display[13],Aip_650_numb_display[1],Aip_650_numb_display[1]);
		
		}

}
void comm_err_detection(void){
	if(uart_wait_err_flg==1){
		
		tx_buff_err=tx_buff_err|0x80;
//		uart_wait_err_flg=0;
//		Aip650_all_close();
		AiP33624_all_close();
		
		diffuse_count1=0;//扩散循环转扩散一次 计数清零
		atuo_count1=0;//圆环自动转圆环单次计数清零
		atuo_count=0;
		ring_delay_count_end_flg=0;		
		flow_delay_count_end_flg=0;						
		ring_delay_count=0;
		flow_delay_count=0;	
		P25=0;
		P27=0;
	 ring_status_old=0;
	 ring_status=0;
	 key_wind_status=0;
	 key_wind_status_old=0;
	 key_self_status=0;
	 key_self_status_old=0;
	 key_fry_status=0;
	 key_fry_status_old=0;
	 key_inc_status=0;
	 key_inc_status_old=0;
	 key_rec_status=0;
	 key_rec_status_old=0;
	 key_timing_status=0;
	 key_timing_status_old=0;		
		
		Aip33624_ring_auto_show_flg=0;
		Aip33624_diffuse_display_flg=0;
		Aip33624_ring_auto_one_show_flg=0;
		Aip33624_diffuse_one_display_flg=0;
		Aip33624_breath_led_control_flg=0;
		Aip33624_ring_auto_one_show_end_flg=0;		
		memset(rx_buff,0,sizeof(rx_buff));
		memset(rx_buff1,0,sizeof(rx_buff1));
		memset(rx_buff_old,0,sizeof(rx_buff_old));
		AiP650_display(0x00,Aip_650_numb_display[13],Aip_650_numb_display[1],Aip_650_numb_display[1]);
	}
		
}
 uint8_t s_bit_change(uint8_t rxbuff){
	uint8_t s_bit1=0;
	uint8_t s_bit2=0;
	uint8_t s_bit3=0;
	uint8_t s_bit4=0;
	uint8_t rxbuff2=0;
	
	s_bit1=rxbuff&0x01;
	s_bit2=(rxbuff&0x02)>>1;
	s_bit3=(rxbuff&0x04)>>2;
	s_bit4=(rxbuff&0x08)>>3;
	rxbuff2=(rxbuff&0xf0);
	rxbuff2=rxbuff2|(s_bit1<<3);
	rxbuff2=rxbuff2|(s_bit2<<2);
	rxbuff2=rxbuff2|(s_bit3<<1);
	rxbuff2=rxbuff2| s_bit4;
	 return rxbuff2;
}
uint8_t rx_buff_0_flg=0;
uint8_t rx_end_flg=0;
uint8_t Digital_tubes_show_time_interval_flg=0;
uint8_t Digital_tubes_show_time_interval=0;
uint8_t Digital_tubes_show_time_interval_count=0;
uint8_t Digital_tubes_show_time_interval_count1=0;
uint8_t Digital_tubes_show_time_interval_count2=0;
void Digital_tubes_upgrate(void){
	
	if(Digital_tubes_show_time_interval_flg)
	{
		Digital_tubes_show_time_interval+=1;
		
		if(Digital_tubes_show_time_interval>20)
		{
			if(Digital_tubes_show_time_interval_flg==1)
				{
					AiP650_display(s_bit_change(rx_buff[5])&0x7f,s_bit_change(rx_buff[6])&0x7f,s_bit_change(rx_buff[7])&0x7f,s_bit_change(rx_buff[8])&0x7f);
				}
				else if(Digital_tubes_show_time_interval_flg==2)
				{
					AiP650_display(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
				}
				else if(Digital_tubes_show_time_interval_flg==3)
				{
					AiP650_display_low(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
				}
					Digital_tubes_show_time_interval_flg=0;
					Digital_tubes_show_time_interval=0;
		}

	}


}
uint8_t uart_reset_flg=0;

void comm_process(void)
{
	int i=0;
	int i2=0;
	comm_err_detection();
	comm_err_detection2();
	comm_200ms_wait_time++;
	if(comm_200ms_wait_time>=comm_200ms_wait_time_set)
	{

		comm_auto_tran();


		comm_200ms_wait_time=0;
	}
	if(EventListener(KEY_STIR_FRY_SHORT))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_short_flg=1;
			button_short_count=0;
			comm_custom_tran(1,FRY_SHORT);
		}		

		
	}
	else if(EventListener(KEY_SELF_HELP_SHORT))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_short_flg=1;
			button_short_count=0;
			comm_custom_tran(1,SELF_HELP_SHORT);
		}				
	}
	else if(EventListener(KEY_WIND_SPEED_SHORT))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_short_flg=1;
			button_short_count=0;
			comm_custom_tran(1,WIND_SPEED_SHORT);
		}				

		
	}
	else if(EventListener(KEY_TIMING_SHORT))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_short_flg=1;
			button_short_count=0;
			comm_custom_tran(1,TIMING_SHORT);
		}				


	}
	else if(EventListener(KEY_INCREASE_SHORT))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_short_flg=1;
			button_short_count=0;
			comm_custom_tran(1,INCREASE_SHORT);
		}				

	}
	else if(EventListener(KEY_DECREASE_SHORT))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_short_flg=1;
			button_short_count=0;
			comm_custom_tran(1,DECREASE_SHORT);
		}				

	}
	if(EventListener(KEY_STIR_FRY_LONG))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			button_long_flg=1;
			comm_custom_tran(2,FRY_LONG);
		}				

		
	}
	else if(EventListener(KEY_SELF_HELP_LONG))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			comm_200ms_wait_time_set=4;
			button_long_flg=1;
			comm_custom_tran(2,SELF_HELP_LONG);
		}				

		
	}
	else if(EventListener(KEY_WIND_SPEED_LONG))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			comm_200ms_wait_time_set=4;
			button_long_flg=1;
			comm_custom_tran(2,WIND_SPEED_LONG);
		}				

		
	}	
	else if(EventListener(KEY_TIMING_LONG))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			comm_200ms_wait_time_set=4;
			button_long_flg=1;
			comm_custom_tran(2,TIMING_LONG);
		}				


	}
	else if(EventListener(KEY_INCREASE_LONG))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			comm_200ms_wait_time_set=4;
			button_long_flg=1;
			//tran_data.tran_data.Data_buff[1]=0x20;
			comm_custom_tran(2,INCREASE_LONG);
		}				

	}
	else if(EventListener(KEY_DECREASE_LONG))
	{
		if((uart_wait_err_flg==0)&&(uart_wait_err_flg2==0))
		{
			comm_200ms_wait_time_set=4;
			button_long_flg=1;
			//tran_data.tran_data.Data_buff[1]=0x10;
			comm_custom_tran(2,DECREASE_LONG);
		}				

	}	
	
	if(rx_end_flg){
		rx_end_flg=0;
			for(i2;i2<21;i2++)
			{
				if(rx_buff[i2]!=0)
					{
				rx_buff_0_flg=1;
					}
			}
	for(i;i<21;i++)
	{
		if(rx_buff_old[i]!=rx_buff[i])
		{

			if(rx_buff_0_flg)
				{
				rx_buff_0_flg=0;
				data_upgrate_flg=1;
				}
			else
				{
				data_upgrate_flg=0;
				}
			
		
			break;
		}
	}
}

	

	if(data_upgrate_flg==1)
	{
		//串口通讯线错误报警
		if(((rx_buff[4]&0x08)>>3)==0x01)
		{
			if(uart_reset_flg==0)
			{
				uart_reset_flg=1;
				uart_reset();
			}

		}else
		{
				uart_reset_flg=0;
		}

		if(((rx_buff[4]&0x80)>>7)==0x01)
		{
			uart_wait_err_flg2=1;
		}else
		{
			
			uart_wait_err_flg2=0;
			if(uart_wait_err_flg==0)
			{
				if(((rx_buff[4]&0x20)>>5)==0x01)
				{
					P25=1;
					
				}else if(((rx_buff[4]&0x20)>>5)==0x00)
				{
					P25=0;
					
				}
				if(((rx_buff[4]&0x10)>>4)==0x01)
				{
					P27=1;
				}
				else if(((rx_buff[4]&0x10)>>4)==0x00)
				{
					P27=0;
				}

				if(((rx_buff[4]&0x40)>>6)==0x01)
				{
					AiP650_display(s_bit_change(rx_buff[5])&0x7f,(Aip_650_numb_display[14])&0x7f,(Aip_650_numb_display[15])&0x7f,(Aip_650_numb_display[10])&0x7f);
				}
				else if(((rx_buff[4]&0x40)>>6)==0x00)
				{
					
				
						if((rx_buff[6+3]&0x03)==0x00)
							{
	//							if(button_long_flg)
	//							{
	//								if(rx_buff[1]==0x03)
	//								{
	//									Digital_tubes_show_time_interval_count+=1;
	//									if(Digital_tubes_show_time_interval_count>1)
	//										{
	//										AiP650_display(s_bit_change(rx_buff[5])&0x7f,s_bit_change(rx_buff[6])&0x7f,s_bit_change(rx_buff[7])&0x7f,s_bit_change(rx_buff[8])&0x7f);
	//										Digital_tubes_show_time_interval_count=0;
	//										}
	//								}else
	//								{
	//										AiP650_display(s_bit_change(rx_buff[5])&0x7f,s_bit_change(rx_buff[6])&0x7f,s_bit_change(rx_buff[7])&0x7f,s_bit_change(rx_buff[8])&0x7f);
	//										Digital_tubes_show_time_interval_count=0;
	//								}
	//							}else{
	//							
	//										AiP650_display(s_bit_change(rx_buff[5])&0x7f,s_bit_change(rx_buff[6])&0x7f,s_bit_change(rx_buff[7])&0x7f,s_bit_change(rx_buff[8])&0x7f);
	//										Digital_tubes_show_time_interval_count=0;
	//							}
											AiP650_display(s_bit_change(rx_buff[5])&0x7f,s_bit_change(rx_buff[6])&0x7f,s_bit_change(rx_buff[7])&0x7f,s_bit_change(rx_buff[8])&0x7f);


							}
							else if((rx_buff[6+3]&0x03)==0x03)
							{
	//							if(button_long_flg)
	//								{
	//									if(rx_buff[1]==0x03)
	//										{
	//											Digital_tubes_show_time_interval_count1+=1;
	//											if(Digital_tubes_show_time_interval_count1>1)
	//											{
	//												AiP650_display(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
	//												Digital_tubes_show_time_interval_count1=0;
	//											}
	//										}else
	//										{
	//												AiP650_display(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
	//												Digital_tubes_show_time_interval_count1=0;
	//										}

	//								}else
	//								{
	//												AiP650_display(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
	//												Digital_tubes_show_time_interval_count1=0;

	//								}
												AiP650_display(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);


							}else if((rx_buff[6+3]&0x03)==0x01)
							{
	//							if(button_long_flg)
	//							{
	//								if(rx_buff[1]==0x03)
	//									{
	//										Digital_tubes_show_time_interval_count2+=1;
	//										if(Digital_tubes_show_time_interval_count2>1)
	//										{
	//											AiP650_display_low(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
	//											Digital_tubes_show_time_interval_count2=0;
	//										}

	//									}else
	//									{
	//											AiP650_display_low(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
	//											Digital_tubes_show_time_interval_count2=0;
	//									}

	//							}else{
	//							
	//											AiP650_display_low(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);
	//											Digital_tubes_show_time_interval_count2=0;							
	//							
	//							}
												AiP650_display_low(s_bit_change((rx_buff[5])&0x7f)|0x80,s_bit_change((rx_buff[6])&0x7f)|0x80,s_bit_change((rx_buff[7])&0x7f)|0x80,s_bit_change((rx_buff[8])&0x7f)|0x80);


							}		
								


				}


					if(((rx_buff[7+3]&0xc0)>>6)==0x00)
					{
						 //右灶
						 AiP33624_WriteData1(0x20,g4|s1|0xc0,0x00);
					}
					else if(((rx_buff[7+3]&0xc0)>>6)==0x01)
					{
						 //右灶
						 AiP33624_WriteData1(0x20,g4|s1|0xc0,0x7f);
					}
					else if(((rx_buff[7+3]&0xc0)>>6)==0x03)
					{
						 //右灶
						 AiP33624_WriteData1(0x20,g4|s1|0xc0,0xff);
					}

					if(((rx_buff[7+3]&0x30)>>4)==0x00)
						{
						 //左灶
						 AiP33624_WriteData1(0x20,g4|s2|0xc0,0x00);
						}
						else if(((rx_buff[7+3]&0x30)>>4)==0x01)
						{
							 //左灶
							 AiP33624_WriteData1(0x20,g4|s2|0xc0,0x7f);
						}
						else if(((rx_buff[7+3]&0x30)>>4)==0x03)
						{
							 //左灶
							 AiP33624_WriteData1(0x20,g4|s2|0xc0,0xff);
						}
						ring_status=rx_buff[8+3];
						if(ring_status_old!=ring_status)
						{
							if(rx_buff[8+3]==0x01)
								{

									//圆环全亮,光晕全亮
									AiP33624_ring_all_show();
									breath_led_all_show();
									
									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									ring_delay_count_end_flg=0;
									flow_delay_count_end_flg=0;							
									ring_delay_count=0;
									flow_delay_count=0;										
									
									Aip33624_ring_auto_show_flg=0;
									Aip33624_diffuse_display_flg=0;
									Aip33624_ring_auto_one_show_flg=0;
									Aip33624_diffuse_one_display_flg=0;
									Aip33624_breath_led_control_flg=0;
									Aip33624_ring_auto_one_show_end_flg=0;						
								}
								else if(rx_buff[8+3]==0x00)
								{
									//无显示
									Aip33624_ring_all_close();
									breath_led_all_close();
									
									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									ring_delay_count_end_flg=0;
									flow_delay_count_end_flg=0;							
									ring_delay_count=0;
									flow_delay_count=0;										
									
									Aip33624_ring_auto_show_flg=0;
									Aip33624_diffuse_display_flg=0;
									Aip33624_ring_auto_one_show_flg=0;
									Aip33624_diffuse_one_display_flg=0;
									Aip33624_breath_led_control_flg=0;
									Aip33624_ring_auto_one_show_end_flg=0;								
								}
								else if(rx_buff[8+3]==0x02)
								{
									//圆环全亮，光晕不亮
									AiP33624_ring_all_show();
									breath_led_all_close();

									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									ring_delay_count_end_flg=0;
									flow_delay_count_end_flg=0;							
									ring_delay_count=0;
									flow_delay_count=0;		
									
									Aip33624_ring_auto_show_flg=0;
									Aip33624_diffuse_display_flg=0;
									Aip33624_ring_auto_one_show_flg=0;
									Aip33624_diffuse_one_display_flg=0;
									Aip33624_breath_led_control_flg=0;
									Aip33624_ring_auto_one_show_end_flg=0;
								}
								else if(rx_buff[8+3]==0x03){
									//圆环转一圈熄灭，光晕扩散一次后常亮
									Aip33624_ring_all_close();
									breath_led_all_close();	

									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									ring_delay_count_end_flg=0;		
									flow_delay_count_end_flg=0;						
									ring_delay_count=0;
									flow_delay_count=0;						
									
									Aip33624_ring_auto_show_flg=1;
									Aip33624_diffuse_display_flg=0;

									Aip33624_ring_auto_one_show_flg=1;
									Aip33624_diffuse_one_display_flg=1;
									Aip33624_breath_led_control_flg=0;
									
									Aip33624_ring_auto_one_show_end_flg=0;
								}
								else if(rx_buff[8+3]==0x04){
									//圆环不亮，光晕扩散一次后循环
									Aip33624_ring_all_close();
									breath_led_all_close();
									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									ring_delay_count_end_flg=0;
									flow_delay_count_end_flg=0;						
									ring_delay_count=0;
									flow_delay_count=0;						
									
									Aip33624_ring_auto_show_flg=0;
									Aip33624_diffuse_display_flg=1;
									Aip33624_ring_auto_one_show_flg=0;
									Aip33624_diffuse_one_display_flg=0;
									Aip33624_breath_led_control_flg=0;
									Aip33624_ring_auto_one_show_end_flg=0;
								}else if(rx_buff[8+3]==0x05){
									//圆环旋转，光晕不亮
									breath_led_all_close();	
									Aip33624_ring_all_close();
									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									ring_delay_count_end_flg=0;
									flow_delay_count_end_flg=0;							
									ring_delay_count=0;
									flow_delay_count=0;
									
									Aip33624_ring_auto_show_flg=1;
									Aip33624_diffuse_display_flg=0;
									Aip33624_ring_auto_one_show_flg=0;
									Aip33624_diffuse_one_display_flg=0;
									Aip33624_breath_led_control_flg=0;
									Aip33624_ring_auto_one_show_end_flg=0;
								}else if(rx_buff[8+3]==0x06){
									//圆环呼吸，光晕呼吸
									breath_led_all_close();	
									Aip33624_ring_all_close();
									diffuse_count1=0;//扩散循环转扩散一次 计数清零
									atuo_count1=0;//圆环自动转圆环单次计数清零
									atuo_count=0;
									
									breath_time=0;
									breath_number=0;
									breath_time1=0;
									breath_number1=0;								
									
									ring_delay_count_end_flg=0;
									flow_delay_count_end_flg=0;							
									ring_delay_count=0;
									flow_delay_count=0;						
									
									Aip33624_ring_auto_show_flg=0;
									Aip33624_diffuse_display_flg=0;
									Aip33624_ring_auto_one_show_flg=0;
									Aip33624_diffuse_one_display_flg=0;	
									Aip33624_breath_led_control_flg=1;	
									Aip33624_ring_auto_one_show_end_flg=0;						
								}
							
							ring_status_old=ring_status;
						}

				key_wind_status=(rx_buff[9+3]&0xc0)>>6;
						if(key_wind_status_old!=key_wind_status)
							{
											//风速键
								if(((rx_buff[9+3]&0xc0)>>6)==0x00)
								{
									//风速4
									AiP33624_WriteData1(0x20,button_numb_display[11]|0xc0,0x00);
									AiP33624_WriteData1(0x20,button_numb_display[12]|0xc0,0x00);
									AiP33624_WriteData1(0x20,button_numb_display[13]|0xc0,0x00);
								}
								else if(((rx_buff[9+3]&0xc0)>>6)==0x01)
								{
									//风速4
									AiP33624_WriteData1(0x20,button_numb_display[11]|0xc0,0x20);//0x3f
									AiP33624_WriteData1(0x20,button_numb_display[12]|0xc0,0x20);
									AiP33624_WriteData1(0x20,button_numb_display[13]|0xc0,0x20);
								}
								else if(((rx_buff[9+3]&0xc0)>>6)==0x03)
								{
									//风速4
									AiP33624_WriteData1(0x20,button_numb_display[11]|0xc0,0xff);
									AiP33624_WriteData1(0x20,button_numb_display[12]|0xc0,0xff);
									AiP33624_WriteData1(0x20,button_numb_display[13]|0xc0,0xff);
								}
									key_wind_status_old=key_wind_status;
						}
				

						key_timing_status=((rx_buff[9+3]&0x30)>>4);
						if(key_timing_status_old!=key_timing_status)
							{
										//定时键
								if(((rx_buff[9+3]&0x30)>>4)==0x00)
								{
									//定时 3
									AiP33624_WriteData1(0x20,button_numb_display[8]|0xc0,0x00);
									AiP33624_WriteData1(0x21,button_numb_display[9]|0xc0,0x00);
									AiP33624_WriteData1(0x21,button_numb_display[10]|0xc0,0x00);
								}
								else if(((rx_buff[9+3]&0x30)>>4)==0x01)
								{
									//定时 3
									AiP33624_WriteData1(0x20,button_numb_display[8]|0xc0,0x20);
									AiP33624_WriteData1(0x21,button_numb_display[9]|0xc0,0x20);
									AiP33624_WriteData1(0x21,button_numb_display[10]|0xc0,0x20);
								}
								else if(((rx_buff[9+3]&0x30)>>4)==0x03)
								{
									//定时 3
									AiP33624_WriteData1(0x20,button_numb_display[8]|0xc0,0xff);
									AiP33624_WriteData1(0x21,button_numb_display[9]|0xc0,0xff);
									AiP33624_WriteData1(0x21,button_numb_display[10]|0xc0,0xff);
								}
									key_timing_status_old=key_timing_status;
						}
						

						key_self_status=((rx_buff[9+3]&0x0c)>>2);
						if(key_self_status_old!=key_self_status)
							{
											//自动键
								if(((rx_buff[9+3]&0x0c)>>2)==0x00)
								{
									//自助 1
									AiP33624_WriteData1(0x20,button_numb_display[2]|0xc0,0x00);
									AiP33624_WriteData1(0x21,button_numb_display[3]|0xc0,0x00);
									AiP33624_WriteData1(0x21,button_numb_display[4]|0xc0,0x00);
								}
								else if(((rx_buff[9+3]&0x0c)>>2)==0x01)
								{
									//自助 1
									AiP33624_WriteData1(0x20,button_numb_display[2]|0xc0,0x20);
									AiP33624_WriteData1(0x21,button_numb_display[3]|0xc0,0x20);
									AiP33624_WriteData1(0x21,button_numb_display[4]|0xc0,0x20);
								}
								else if(((rx_buff[9+3]&0x0c)>>2)==0x03)
								{
									//自助 1 
									AiP33624_WriteData1(0x20,button_numb_display[2]|0xc0,0xff);
									AiP33624_WriteData1(0x21,button_numb_display[3]|0xc0,0xff);
									AiP33624_WriteData1(0x21,button_numb_display[4]|0xc0,0xff);
								}
									key_self_status_old=key_self_status;
						}
							
					
							key_fry_status=(rx_buff[9+3]&0x03);
						if(key_fry_status_old!=key_fry_status)
							{
								//爆炒键
							if((rx_buff[9+3]&0x03)==0x00)
							{
								//爆炒 2
								AiP33624_WriteData1(0x20,button_numb_display[5]|0xc0,0x00);
								AiP33624_WriteData1(0x20,button_numb_display[6]|0xc0,0x00);
								AiP33624_WriteData1(0x20,button_numb_display[7]|0xc0,0x00);
							}
							else if((rx_buff[9+3]&0x03)==0x01)
							{
								//爆炒 2
								AiP33624_WriteData1(0x20,button_numb_display[5]|0xc0,0x20);
								AiP33624_WriteData1(0x20,button_numb_display[6]|0xc0,0x20);
								AiP33624_WriteData1(0x20,button_numb_display[7]|0xc0,0x20);
							}
							else if((rx_buff[9+3]&0x03)==0x03)
							{
								//爆炒 2
								AiP33624_WriteData1(0x20,button_numb_display[5]|0xc0,0xff);
								AiP33624_WriteData1(0x20,button_numb_display[6]|0xc0,0xff);
								AiP33624_WriteData1(0x20,button_numb_display[7]|0xc0,0xff);
							}
							key_fry_status_old=key_fry_status;
						}

						key_inc_status=((rx_buff[10+3]&0x0c)>>2);
						if(key_inc_status_old!=key_inc_status)
							{
										//按键加
								if(((rx_buff[10+3]&0x0c)>>2)==0x00)
								{
									//右
									AiP33624_WriteData1(0x20,button_numb_display[0]|0xc0,0x00);
								}
								else if(((rx_buff[10+3]&0x0c)>>2)==0x01)
								{
									//右
									AiP33624_WriteData1(0x20,button_numb_display[0]|0xc0,0x20);
								}
								else if(((rx_buff[10+3]&0x0c)>>2)==0x03)
								{
									//右
									AiP33624_WriteData1(0x20,button_numb_display[0]|0xc0,0xff);
								}
								key_inc_status_old=key_inc_status;
							}
				
						key_rec_status=(rx_buff[10+3]&0x03);
						if(key_rec_status_old!=key_rec_status)
							{
									//按键减
								if((rx_buff[10+3]&0x03)==0x00)
								{
									//左
									AiP33624_WriteData1(0x20,button_numb_display[1]|0xc0,0x00);

								}
								else if((rx_buff[10+3]&0x03)==0x01)
								{
									//左
									AiP33624_WriteData1(0x20,button_numb_display[1]|0xc0,0x20);

								}
								else if((rx_buff[10+3]&0x03)==0x03)
								{
									//左
									AiP33624_WriteData1(0x20,button_numb_display[1]|0xc0,0xff);
								}
								key_rec_status_old=key_rec_status;
							}
					


					memcpy(rx_buff_old,rx_buff,sizeof(rx_buff_old));
					data_upgrate_flg=0;		
			}

			
		}
		
	

	}

	

}

void rec_detection(void){
			
			rx_buff1[rx_count++]=US2CON3;
				if(rx_buff1[0]!=0xaa){
					rx_count=0;
					memset(rx_buff1,0,sizeof(rx_buff1));
				}
					if(rx_count>2)
					{
						if(rx_buff1[2]!=(RX_BUF_SIZE-1))
						{
							rx_count=0;
							memset(rx_buff1,0,sizeof(rx_buff1));
						}
				}
				if(rx_count>=RX_BUF_SIZE)
				{
					rx_check=RX_CheckSum(rx_buff1,RX_BUF_SIZE);
					tx_check=TX_CheckSum1(rx_buff1,RX_BUF_SIZE-1);
					if(rx_check==0&&tx_check==rx_buff1[RX_BUF_SIZE-1]){
						rx_count=0;
						rx_end_flg=1;
						memcpy(rx_buff,rx_buff1,sizeof(rx_buff));
						memset(rx_buff1,0,sizeof(rx_buff1));
						tx_buff_err=0x00;
						uart_wait_time_count=0;
						uart_wait_err_flg=0;
					//	tx_buff_err_count=0;					
					}else{
						rx_count=0;
						rx_end_flg=0;
						memset(rx_buff1,0,sizeof(rx_buff1));
					}
				
				}


								
}